package com.tamartech.greendb.ui.tables;

import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

import org.apache.log4j.Logger;
import org.apache.tools.ant.Project;
import org.springframework.context.ApplicationEvent;
import org.springframework.richclient.application.event.LifecycleApplicationEvent;

import com.tamartech.greendb.dao.ElectoralRollEntryDAO;
import com.tamartech.greendb.data.Ward;
import com.tamartech.greendb.data.ElectoralRoll;
import com.tamartech.greendb.data.ElectoralRollEntry;


public class RollEntryTable extends AbstractTable
{
    private static Logger log = Logger.getLogger(RollEntryTable.class);
    
    private ElectoralRollEntryDAO electoralRollEntryDAO = null;
    
    public void setElectoralRoll(ElectoralRoll electoralRoll)
    {
        this.electoralRoll = electoralRoll;
    }

    public void setConstituency(Ward constituency)
    {
        this.constituency = constituency;
    }

    private ElectoralRoll electoralRoll;
    private Ward constituency;
    
    public RollEntryTable()
    {
        super("rollEntryTable", 
              new String[]
              { 
                   "refPrefix", 
                   "refSuffix",
                   "person.surname",
                   "person.firstNames",
                   "address.address1",
                   "address.address2",
                   "address.address3",
                   "address.address4",
                   "address.address5",
                   "address.postcode",
              });
    }

    @Override
    protected boolean shouldHandleEvent(ApplicationEvent e)
    {
        boolean shouldHandle = false;

        if (e instanceof LifecycleApplicationEvent)
        {
            LifecycleApplicationEvent event = (LifecycleApplicationEvent) e;
            if (event.getObject() instanceof Project)
            {
                shouldHandle = true;
            }
        }
        
        return shouldHandle;
    }

    @Override
    protected void configureTable (JTable table)
    {
        TableColumnModel tcm = table.getColumnModel();
        tcm.getColumn(0).setPreferredWidth(100);
        
//        table.setDefaultRenderer(String.class, new CompanyCellRenderer());
    }

    @Override
    protected Object[] getDefaultInitialData ()
    {
        Object[] data = new Object[0];
        
        try
        {
            data = electoralRollEntryDAO.list().toArray();
        }
        catch (Exception e)
        {
            log.error("Failed to list projects.", e);
        }
        
        return data;
    }
    
    public ElectoralRollEntry getSelectedRollEntry()
    {
        ElectoralRollEntry e = null;
        
        int selected = getTable().getSelectedRow();
        
        if (selected > -1)
            e = (ElectoralRollEntry) getTableModel().getElementAt(selected);
        
        return e;
    }
    
    public void setElectoralRollEntryDAO (ElectoralRollEntryDAO electoralRollEntryDAO)
    {
        this.electoralRollEntryDAO = electoralRollEntryDAO;
    }
}
